package mju.cxf.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mju.cxf.constant.MessageConstant;
import mju.cxf.constant.PasswordConstant;
import mju.cxf.dto.AdminDTO;
import mju.cxf.dto.CategoriesDTO;
import mju.cxf.entity.Admin;
import mju.cxf.entity.Categories;
import mju.cxf.result.Result;
import mju.cxf.service.CategoriesService;
import mju.cxf.vo.CategoriesVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 分类相关接口
 */

@RestController
@RequestMapping("/admin/api/categories")
@Api(tags = "分类接口列表")
public class CategoriesController {
    private static final Logger log = LoggerFactory.getLogger(CategoriesController.class);
    @Autowired
    private CategoriesService categoriesService;

    /**
     * 新建分类信息
     *
     * @param categories
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新建分类")
    public Result<String> createCategories(@RequestBody CategoriesDTO categories) {
        return categoriesService.saveCategories(categories) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 删除分类信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除分类信息", notes = "管理员通过此接口删除分类信息")
    public Result<String> deleteCategories(@PathVariable Integer id) {
        return categoriesService.deleteCategories(id) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 修改分类信息
     *
     * @param categoriesDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改分类信息")
    public Result<String> updateAdmin(@RequestBody CategoriesDTO categoriesDTO) {
        return categoriesService.updateCategories(categoriesDTO) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 批量删除分类信息
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/batch")
    @ApiOperation(value = "批量删除分类信息", notes = "管理员通过此接口批量删除分类信息")
    public Result<String> deleteCategorieses(@RequestBody List<Integer> ids) {
        return categoriesService.deleteCategorieses(ids) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 分页查询分类信息
     * @return 分页查询结果
     */
    @GetMapping("/pages")
    @ApiOperation(value = "分页查询分类信息", notes = "管理员通过此接口分页查询分类信息")
    public Page<CategoriesVO> getUsersByName(@RequestParam(value = "name",required = false) String name,
                                             @RequestParam(value = "type",required = false) Integer type,
                                             @RequestParam(value = "state",required = false) Integer state,
                                             @RequestParam(value = "current",defaultValue = "1") int current,
                                             @RequestParam(value = "size",defaultValue = "10") int size) {
        return categoriesService.selectPage(name, type, state, current, size);
    }
}
